package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.CuisineDao;
import com.cn.wanxi.model.CuisineModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CuisineImpl implements CuisineDao {
    @Override
    public int add(CuisineModel cuisineModel) {
        return 0;
    }

    @Override
    public int update(CuisineModel cuisineModel) {
        return 0;
    }

    @Override
    public int delete(CuisineModel cuisineModel) {
        return 0;
    }

    @Override
    public List<CuisineModel> findAll(CuisineModel cuisineModel) {
        String sql = "select * from cuisine where true ";
        List listSql = new ArrayList();
        if (cuisineModel.getIsShow() != -1) {
            sql += " and is_show=?";
            listSql.add(cuisineModel.getIsShow());
        }
        sql += " order by sort";
        List<CuisineModel> list = new ArrayList<>();
        ResultSet resultSet = JDBC.query(sql, listSql);
        try {
            while (resultSet.next()) {
                CuisineModel model = new CuisineModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setCreatTime(resultSet.getString("create_time"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public CuisineModel findById(CuisineModel cuisineModel) {
        return null;
    }

    @Override
    public int getCount(CuisineModel cuisineModel) {
        String sql = "select count(*) count from cuisine  where true";
        List list = new ArrayList();
        if (cuisineModel.getIsShow() != -1) {
            sql += " and is_show=?";
            list.add(cuisineModel.getIsShow());
        }

        ResultSet resultSet = JDBC.query(sql, list);
        try {
            while (resultSet.next()) {
                return resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int changeEnable(CuisineModel cuisineModel) {
        return 0;
    }
}
